package com.whmall.service.crm.module.suppliercreditscore.service.impl;

import com.whmall.component.util.NumberUtils;
import com.whmall.service.crm.entity.dto.suppliercreditscore.SupplierCreditScoreSettingReqDTO;
import com.whmall.service.crm.entity.dto.suppliercreditscore.SupplierCreditScoreSettingRspDTO;
import com.whmall.service.crm.module.suppliercreditscore.SupplierCreditScoreSetting;
import com.whmall.service.crm.module.suppliercreditscore.service.SupplierCreditScoreSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author：yangdong
 * Date：2020/12/12
 * Description：
 */
@Service
@Slf4j
public class SupplierCreditScoreSettingServiceImpl implements SupplierCreditScoreSettingService {

    private static final String SUPPLIER_CREDIT_SCORE_SETTING_REDIS_KEY = "$supplier_credit_score_setting";

    @Autowired
    private RedisTemplate<String, SupplierCreditScoreSetting> redisTemplate;

    @Override
    public List<SupplierCreditScoreSettingRspDTO> list() {
        List<SupplierCreditScoreSetting> settingList = getAll();
        List<SupplierCreditScoreSettingRspDTO> rspDTOList = settingList.stream()
                .filter(SupplierCreditScoreSetting::getIsEnabled)
                .map(e -> {
                    SupplierCreditScoreSettingRspDTO rspDTO = new SupplierCreditScoreSettingRspDTO();
                    BeanUtils.copyProperties(e, rspDTO);
                    rspDTO.setQuota(NumberUtils.divide100(e.getQuota()));
                    rspDTO.setIndex(settingList.indexOf(e));
                    return rspDTO;
                })
                .sorted(Comparator.comparing(SupplierCreditScoreSettingRspDTO::getMinScore))
                .collect(Collectors.toList());

        return rspDTOList;
    }

    @Override
    public void create(SupplierCreditScoreSettingReqDTO reqDTO, Long operatorId) {
        SupplierCreditScoreSetting supplierCreditScoreSetting = new SupplierCreditScoreSetting();
        BeanUtils.copyProperties(reqDTO, supplierCreditScoreSetting);
        supplierCreditScoreSetting.setQuota(NumberUtils.multiply100(reqDTO.getQuota()));
        supplierCreditScoreSetting.setIsEnabled(true);
        supplierCreditScoreSetting.setUpdateId(operatorId);
        supplierCreditScoreSetting.setUpdateTime(new Date());
        redisTemplate.opsForList().rightPush(SUPPLIER_CREDIT_SCORE_SETTING_REDIS_KEY, supplierCreditScoreSetting);
    }

    @Override
    public void update(Integer index, SupplierCreditScoreSettingReqDTO reqDTO, Long operatorId) {
        SupplierCreditScoreSetting supplierCreditScoreSetting = new SupplierCreditScoreSetting();
        BeanUtils.copyProperties(reqDTO, supplierCreditScoreSetting);
        supplierCreditScoreSetting.setIsEnabled(true);
        supplierCreditScoreSetting.setQuota(NumberUtils.multiply100(reqDTO.getQuota()));
        supplierCreditScoreSetting.setUpdateId(operatorId);
        supplierCreditScoreSetting.setUpdateTime(new Date());
        redisTemplate.opsForList().set(SUPPLIER_CREDIT_SCORE_SETTING_REDIS_KEY, index, supplierCreditScoreSetting);
    }

    @Override
    public List<SupplierCreditScoreSetting> getAll() {
        return redisTemplate.opsForList().range(SUPPLIER_CREDIT_SCORE_SETTING_REDIS_KEY, 0, -1);
    }
}
